﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;

namespace YXShop.Web.admin.templates.lable
{
    public partial class label_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("011002001");
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormInt("id") > 0)
                {
                    YXShop.Common.PromptInfo.Popedom("011002003","对不起，您没有权限进行删除");
                    string types = Request.Form["Option"].Trim();
                    int id = ChangeHope.WebPage.PageRequest.GetFormInt("id");
                    if (types == "del")
                    {
                        string path = ChangeHope.WebPage.PageRequest.GetFormString("path");
                        del(id,path);
                    }
                    Response.End();
                    return;
                }
                string isStoreLabel= ChangeHope.WebPage.PageRequest.GetQueryInt("w_d_isstorelabel").ToString();
                string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("q_templatepath");
                this.w_d_isstorelabel.Value =isStoreLabel;
                this.hlback.NavigateUrl = "labelclass_list.aspx?w_d_isstorelabel=" + isStoreLabel + "&q_templatepath=" + templatePath + "";
                this.lbtitle.Text = ChangeHope.WebPage.PageRequest.GetQueryString("q_name");
                string styleId = ChangeHope.WebPage.PageRequest.GetString("q_styleId");
                ViewState["StyleId"] = styleId;
                GetList(styleId);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        protected void GetList(string StyleId)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            //YXShop.BLL.Templates.Label data = new YXShop.BLL.Templates.Label();
            //ChangeHope.DataBase.DataByPage dataPage = data.GetList();
            //第一步先添加表头
            table.AddHeadCol("5%", "序号");
            table.AddHeadCol("55%", "标签名称");
            //table.AddHeadCol("20%", "标签类型");
            table.AddHeadCol("20%", "更新时间");
            table.AddHeadCol("20%", "操作");
            table.AddRow();
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("q_templatepath");
            string typeId = ChangeHope.WebPage.PageRequest.GetQueryString("q_id");
            string path = Server.MapPath("~//" + templatePath + "/label/label.xml");
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNode xn = xmlDoc.SelectSingleNode("templetelabel");
                XmlNodeList xnl = xn.ChildNodes;
                if (xnl.Count > 0)
                {
                    int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                    if (curpage < 0)
                    {
                        curpage = 1;
                    }
                    int count = 0;
                    foreach (XmlNode xnf in xnl)
                    {
                        XmlElement xe = (XmlElement)xnf;
                        XmlNodeList xnf1 = xe.ChildNodes;
                        bool isContinue = false;
                        foreach (XmlNode xn2 in xnf1)
                        {
                            if (xn2.Name == "labeltype" && xn2.InnerText == typeId)
                            {
                                isContinue = true;
                                break;
                            }
                        }
                        if (!isContinue)
                        {
                            continue;
                        }
                        count++;
                        string No = (15 * (curpage - 1) + count).ToString();
                        table.AddCol(No);
                        string labelName = xe.GetAttribute("Name");
                        string labelId = xe.GetAttribute("Id");
                        string moduleId = "";
                        string labelTypeId = "";
                        table.AddCol(string.Format("{0}", labelName));
                        foreach (XmlNode xn2 in xnf1)
                        {
                            if (xn2.Name == "creattime")
                            {
                                table.AddCol(xn2.InnerText);
                                continue;
                            }
                            if (xn2.Name == "moduleid")
                            {
                                moduleId = xn2.InnerText;
                                continue;
                            }
                            if (xn2.Name == "labeltype")
                            {
                                labelTypeId = xn2.InnerText;
                                continue;
                            }
                        }

                        table.AddCol(string.Format("<a href=label_edit.aspx?name={0}&moduleid={1}&tid={2}&FilePath={4}>编辑</a> <a href='#' onclick=\"Del({3},'{4}')\">删除</a>", labelName, moduleId, labelTypeId, labelId, templatePath));
                        table.AddRow();
                    }

                }
            }

            //添加表的内容
            //if (dataPage.DataReader != null)
            //{
            //    int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
            //    if (curpage < 0)
            //    {
            //        curpage = 1;
            //    }
            //    int count = 0;
            //    while (dataPage.DataReader.Read())
            //    {
            //        count++;
            //        string No = (15 * (curpage - 1) + count).ToString();
            //        table.AddCol(No);
            //        table.AddCol(string.Format("{0}", dataPage.DataReader["name"].ToString()));
            //        table.AddCol(string.Format("{0}", ""));
            //        table.AddCol(dataPage.DataReader["createtime"].ToString());
            //        if (dataPage.DataReader["tid"].ToString() == "1")
            //        {
            //            table.AddCol(string.Format("<a href=label_edit.aspx?StyleId=" + StyleId + "&name={0}&moduleid={1}&tid={2}>编辑</a>", dataPage.DataReader["name"].ToString(), dataPage.DataReader["moduleid"].ToString(), dataPage.DataReader["tid"].ToString()));
            //        }
            //        else
            //        {
            //            table.AddCol(string.Format("<a href=label_edit.aspx?StyleId=" + StyleId + "&name={0}&moduleid={1}&tid={2}>编辑</a> <a href='#' onclick='Del({3})'>删除</a>", dataPage.DataReader["name"].ToString(), dataPage.DataReader["moduleid"].ToString(), dataPage.DataReader["tid"].ToString(), dataPage.DataReader["id"].ToString()));
            //        }
            //        table.AddRow();
            //    }
            //} + dataPage.PageToolBar
            string view = table.GetTable();
            //dataPage.Dispose();
            //dataPage = null;
            this.Literal1.Text = view;
        }

        protected void lbSearch_Click(object sender, EventArgs e)
        {
            string styleId = ViewState["StyleId"].ToString();
            GetList(styleId);
        }
        protected void del(int id,string templatePath)
        {
            string path = Server.MapPath("~//" + templatePath + "/label/label.xml");
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNode root = xmlDoc.SelectSingleNode("templetelabel");
                XmlNodeList xnList = root.ChildNodes;
                foreach (XmlNode xn in xnList)
                {
                    XmlElement xe = (XmlElement)xn;
                    string nodeId = xe.GetAttribute("Id");
                    if (nodeId == id.ToString())
                    {
                        root.RemoveChild(xn);// 移除指定的子节点
                        break;
                    }
                }
                xmlDoc.Save(path);
                Response.Write("ok");
            }
        }
    }
}

